<template>
  <div class="property-registration">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2 class="page-title">财产登记管理</h2>
      <p class="page-description">管理水库相关的各类财产资产信息</p>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-section">
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><OfficeBuilding /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statsData.totalAssets }}</div>
              <div class="stat-label">总资产数量</div>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><Monitor /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statsData.totalEquipment }}</div>
              <div class="stat-label">设备数量</div>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><MapLocation /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statsData.totalLand }}</div>
              <div class="stat-label">土地面积(亩)</div>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon">
              <el-icon><Money /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ formatAmount(statsData.totalValue) }}</div>
              <div class="stat-label">总价值(万元)</div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <el-tabs v-model="activeTab" type="card" @tab-change="handleTabChange">
        <!-- 固定资产管理 -->
        <el-tab-pane label="固定资产" name="assets">
          <div class="tab-content">
            <!-- 查询条件 -->
            <el-card shadow="hover" style="margin-bottom: 8px">
              <el-form :model="assetQuery" inline>
                <el-form-item label="资产名称">
                  <el-input
                    v-model="assetQuery.name"
                    placeholder="请输入资产名称"
                    clearable
                    style="width: 200px"
                  />
                </el-form-item>
                <el-form-item label="资产类型">
                  <el-select v-model="assetQuery.type" placeholder="请选择类型" clearable style="width: 150px">
                    <el-option label="建筑物" value="building" />
                    <el-option label="构筑物" value="structure" />
                    <el-option label="机械设备" value="machinery" />
                    <el-option label="运输设备" value="transport" />
                    <el-option label="其他" value="other" />
                  </el-select>
                </el-form-item>
                <el-form-item label="使用状态">
                  <el-select v-model="assetQuery.status" placeholder="请选择状态" clearable style="width: 120px">
                    <el-option label="正常使用" value="normal" />
                    <el-option label="维修中" value="maintenance" />
                    <el-option label="闲置" value="idle" />
                    <el-option label="报废" value="scrapped" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :icon="Search" @click="searchAssets">查询</el-button>
                  <el-button :icon="Refresh" @click="resetAssetQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </el-card>

            <!-- 操作按钮 -->
            <el-card shadow="never">
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button type="primary" plain icon="Plus" @click="handleAddAsset">新增资产</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button type="success" plain icon="Download" @click="exportAssets">导出数据</el-button>
                </el-col>
              </el-row>
            </el-card>

            <!-- 数据表格 -->
            <el-card shadow="hover">
              <el-table :data="assetList" border style="width: 100%">
                <el-table-column prop="code" label="资产编号" width="120" />
                <el-table-column prop="name" label="资产名称" width="200" />
                <el-table-column prop="type" label="资产类型" width="120">
                  <template #default="{ row }">
                    <el-tag :type="getAssetTypeTag(row.type)">
                      {{ getAssetTypeText(row.type) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="specification" label="规格型号" width="150" />
                <el-table-column prop="purchaseDate" label="购置日期" width="120" />
                <el-table-column prop="originalValue" label="原值(万元)" width="120">
                  <template #default="{ row }">
                    <span class="amount">{{ formatAmount(row.originalValue) }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="currentValue" label="现值(万元)" width="120">
                  <template #default="{ row }">
                    <span class="amount">{{ formatAmount(row.currentValue) }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="status" label="使用状态" width="100">
                  <template #default="{ row }">
                    <el-tag :type="getAssetStatusTag(row.status)">
                      {{ getAssetStatusText(row.status) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="location" label="存放位置"/>
                <el-table-column label="操作" width="290" fixed="right">
                  <template #default="{ row }">
                    <el-button link type="primary" icon="View" @click="viewAssetDetail(row)">查看</el-button>
                    <el-button link type="primary" icon="Edit" @click="editAsset(row)">编辑</el-button>
                    <el-button link type="info" icon="Document" @click="viewAssetHistory(row)">履历</el-button>
                    <el-button link type="danger" icon="Delete" @click="deleteAsset(row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

              <!-- 分页 -->
              <div class="pagination-section">
                <Pagination
                  :total="assetPagination.total"
                  v-model:page="assetPagination.currentPage"
                  v-model:limit="assetPagination.pageSize"
                  @pagination="handleAssetPagination"
                />
              </div>
            </el-card>
          </div>
        </el-tab-pane>

        <!-- 设备管理 -->
        <el-tab-pane label="设备管理" name="equipment">
          <div class="tab-content">
            <!-- 查询条件 -->
            <div class="query-section">
              <el-form :model="equipmentQuery" inline>
                <el-form-item label="设备名称">
                  <el-input
                    v-model="equipmentQuery.name"
                    placeholder="请输入设备名称"
                    clearable
                    style="width: 200px"
                  />
                </el-form-item>
                <el-form-item label="设备类型">
                  <el-select v-model="equipmentQuery.type" placeholder="请选择类型" clearable style="width: 150px">
                    <el-option label="监测设备" value="monitoring" />
                    <el-option label="控制设备" value="control" />
                    <el-option label="通信设备" value="communication" />
                    <el-option label="安全设备" value="safety" />
                    <el-option label="其他设备" value="other" />
                  </el-select>
                </el-form-item>
                <el-form-item label="运行状态">
                  <el-select v-model="equipmentQuery.status" placeholder="请选择状态" clearable style="width: 120px">
                    <el-option label="正常运行" value="running" />
                    <el-option label="故障" value="fault" />
                    <el-option label="维护中" value="maintenance" />
                    <el-option label="停用" value="stopped" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :icon="Search" @click="searchEquipment">查询</el-button>
                  <el-button :icon="Refresh" @click="resetEquipmentQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </div>

            <!-- 操作按钮 -->
            <el-card shadow="never">
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button type="primary" plain icon="Plus" @click="handleAddEquipment">新增设备</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button type="success" plain icon="Download" @click="exportEquipment">导出数据</el-button>
                </el-col>
              </el-row>
            </el-card>

            <!-- 数据表格 -->
            <el-card shadow="hover">
              <el-table :data="equipmentList" border style="width: 100%">
                <el-table-column prop="code" label="设备编号" width="120" />
                <el-table-column prop="name" label="设备名称" width="200" />
                <el-table-column prop="type" label="设备类型" width="120">
                  <template #default="{ row }">
                    <el-tag :type="getEquipmentTypeTag(row.type)">
                      {{ getEquipmentTypeText(row.type) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="model" label="型号规格" width="150" />
                <el-table-column prop="manufacturer" label="生产厂家" width="150" />
                <el-table-column prop="installDate" label="安装日期" width="120" />
                <el-table-column prop="warrantyPeriod" label="保修期(月)" width="100" />
                <el-table-column prop="status" label="运行状态" width="100">
                  <template #default="{ row }">
                    <el-tag :type="getEquipmentStatusTag(row.status)">
                      {{ getEquipmentStatusText(row.status) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="location" label="安装位置" />
                <el-table-column label="操作" width="250" fixed="right">
                  <template #default="{ row }">
                    <el-button link type="primary" icon="View" @click="viewEquipmentDetail(row)">查看</el-button>
                    <el-button link type="primary" icon="Edit" @click="editEquipment(row)">编辑</el-button>
                    <el-button link type="info" icon="Document" @click="viewMaintenanceRecord(row)">维护记录</el-button>
                    <el-button link type="danger" icon="Delete" @click="deleteEquipment(row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

              <!-- 分页 -->
              <div class="pagination-section">
                <Pagination
                  :total="equipmentPagination.total"
                  v-model:page="equipmentPagination.currentPage"
                  v-model:limit="equipmentPagination.pageSize"
                  @pagination="handleEquipmentPagination"
                />
              </div>
            </el-card>
          </div>
        </el-tab-pane>

        <!-- 土地管理 -->
        <el-tab-pane label="土地管理" name="land">
          <div class="tab-content">
            <!-- 查询条件 -->
            <div class="query-section">
              <el-form :model="landQuery" inline>
                <el-form-item label="地块名称">
                  <el-input
                    v-model="landQuery.name"
                    placeholder="请输入地块名称"
                    clearable
                    style="width: 200px"
                  />
                </el-form-item>
                <el-form-item label="土地性质">
                  <el-select v-model="landQuery.nature" placeholder="请选择性质" clearable style="width: 150px">
                    <el-option label="国有土地" value="state_owned" />
                    <el-option label="集体土地" value="collective" />
                    <el-option label="租赁土地" value="leased" />
                  </el-select>
                </el-form-item>
                <el-form-item label="使用状态">
                  <el-select v-model="landQuery.status" placeholder="请选择状态" clearable style="width: 120px">
                    <el-option label="正常使用" value="normal" />
                    <el-option label="闲置" value="idle" />
                    <el-option label="争议中" value="disputed" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :icon="Search" @click="searchLand">查询</el-button>
                  <el-button :icon="Refresh" @click="resetLandQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </div>

            <!-- 操作按钮 -->
            <el-card shadow="never">
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button type="primary" plain icon="Plus" @click="handleAddLand">新增地块</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button type="success" plain icon="Download" @click="exportLand">导出数据</el-button>
                </el-col>
              </el-row>
            </el-card>

            <!-- 数据表格 -->
            <el-card shadow="hover">
              <el-table :data="landList" border style="width: 100%">
                <el-table-column prop="code" label="地块编号" width="120" />
                <el-table-column prop="name" label="地块名称" width="200" />
                <el-table-column prop="nature" label="土地性质" width="120">
                  <template #default="{ row }">
                    <el-tag :type="getLandNatureTag(row.nature)">
                      {{ getLandNatureText(row.nature) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="area" label="面积(亩)" width="100" />
                <el-table-column prop="location" label="地理位置"/>
                <el-table-column prop="useType" label="用途" width="120" />
                <el-table-column prop="certificateNumber" label="证书编号" width="150" />
                <el-table-column prop="acquisitionDate" label="取得日期" width="120" />
                <el-table-column prop="status" label="使用状态" width="100">
                  <template #default="{ row }">
                    <el-tag :type="getLandStatusTag(row.status)">
                      {{ getLandStatusText(row.status) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="240" fixed="right">
                  <template #default="{ row }">
                    <el-button link type="primary" icon="View" @click="viewLandDetail(row)">查看</el-button>
                    <el-button link type="primary" icon="Edit" @click="editLand(row)">编辑</el-button>
                    <el-button link type="info" icon="Document" @click="viewLandCertificate(row)">证书</el-button>
                    <el-button link type="danger" icon="Delete" @click="deleteLand(row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

              <!-- 分页 -->
              <div class="pagination-section">
                <Pagination
                  :total="landPagination.total"
                  v-model:page="landPagination.currentPage"
                  v-model:limit="landPagination.pageSize"
                  @pagination="handleLandPagination"
                />
              </div>
            </el-card>
          </div>
        </el-tab-pane>

        <!-- 无形资产 -->
        <el-tab-pane label="无形资产" name="intangible">
          <div class="tab-content">
            <!-- 查询条件 -->
            <div class="query-section">
              <el-form :model="intangibleQuery" inline>
                <el-form-item label="资产名称">
                  <el-input
                    v-model="intangibleQuery.name"
                    placeholder="请输入资产名称"
                    clearable
                    style="width: 200px"
                  />
                </el-form-item>
                <el-form-item label="资产类型">
                  <el-select v-model="intangibleQuery.type" placeholder="请选择类型" clearable style="width: 150px">
                    <el-option label="软件著作权" value="software" />
                    <el-option label="专利权" value="patent" />
                    <el-option label="商标权" value="trademark" />
                    <el-option label="土地使用权" value="land_use" />
                    <el-option label="其他" value="other" />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :icon="Search" @click="searchIntangible">查询</el-button>
                  <el-button :icon="Refresh" @click="resetIntangibleQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </div>

            <!-- 操作按钮 -->
            <el-card shadow="never">
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button type="primary" plain icon="Plus" @click="handleAddIntangible">新增无形资产</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button type="success" plain icon="Download" @click="exportIntangible">导出数据</el-button>
                </el-col>
              </el-row>
            </el-card>

            <!-- 数据表格 -->
            <el-card shadow="hover">
              <el-table :data="intangibleList" border style="width: 100%">
                <el-table-column prop="code" label="资产编号" width="120" />
                <el-table-column prop="name" label="资产名称" />
                <el-table-column prop="type" label="资产类型" width="120">
                  <template #default="{ row }">
                    <el-tag :type="getIntangibleTypeTag(row.type)">
                      {{ getIntangibleTypeText(row.type) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="acquisitionDate" label="取得日期" width="120" />
                <el-table-column prop="originalValue" label="原值(万元)" width="120">
                  <template #default="{ row }">
                    <span class="amount">{{ formatAmount(row.originalValue) }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="amortizationPeriod" label="摊销期(年)" width="100" />
                <el-table-column prop="remainingValue" label="净值(万元)" width="120">
                  <template #default="{ row }">
                    <span class="amount">{{ formatAmount(row.remainingValue) }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="expiryDate" label="到期日期" width="120" />
                <el-table-column prop="status" label="状态" width="100">
                  <template #default="{ row }">
                    <el-tag :type="getIntangibleStatusTag(row.status)">
                      {{ getIntangibleStatusText(row.status) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="240" fixed="right">
                  <template #default="{ row }">
                    <el-button link type="primary" icon="View" @click="viewIntangibleDetail(row)">查看</el-button>
                    <el-button link type="primary" icon="Edit" @click="editIntangible(row)">编辑</el-button>
                    <el-button link type="info" icon="Document" @click="viewIntangibleCertificate(row)">证书</el-button>
                    <el-button link type="danger" icon="Delete" @click="deleteIntangible(row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

              <!-- 分页 -->
              <div class="pagination-section">
                <Pagination
                  :total="intangiblePagination.total"
                  v-model:page="intangiblePagination.currentPage"
                  v-model:limit="intangiblePagination.pageSize"
                  @pagination="handleIntangiblePagination"
                />
              </div>
            </el-card>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 对话框组件 -->
    <!-- 这里可以添加各种对话框组件 -->
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Search, Plus, Download, Refresh,
  OfficeBuilding, Monitor, MapLocation, Money,
  View, Edit, Delete, Document
} from '@element-plus/icons-vue'
import Pagination from '@/components/Pagination/index.vue'

// 页面数据
const activeTab = ref('assets')

// 统计数据
const statsData = reactive({
  totalAssets: 156,
  totalEquipment: 89,
  totalLand: 1250,
  totalValue: 8500
})

// 固定资产相关数据
const assetQuery = reactive({
  name: '',
  type: '',
  status: ''
})

const assetList = ref<any[]>([])
const assetPagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 设备管理相关数据
const equipmentQuery = reactive({
  name: '',
  type: '',
  status: ''
})

const equipmentList = ref<any[]>([])
const equipmentPagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 土地管理相关数据
const landQuery = reactive({
  name: '',
  nature: '',
  status: ''
})

const landList = ref<any[]>([])
const landPagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 无形资产相关数据
const intangibleQuery = reactive({
  name: '',
  type: ''
})

const intangibleList = ref<any[]>([])
const intangiblePagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 加载状态
const equipmentLoading = ref(false)
const landLoading = ref(false)
const intangibleLoading = ref(false)

// 新增：通用随机与持久化数据源
const randInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
const allAssetData = ref<any[]>([])
const allEquipmentData = ref<any[]>([])
const allLandData = ref<any[]>([])
const allIntangibleData = ref<any[]>([])

const applyAssetFiltersAndPaginate = () => {
  let filtered = allAssetData.value.slice()
  if (assetQuery.name) filtered = filtered.filter(item => item.name.includes(assetQuery.name))
  if (assetQuery.type) filtered = filtered.filter(item => item.type === assetQuery.type)
  if (assetQuery.status) filtered = filtered.filter(item => item.status === assetQuery.status)
  assetPagination.total = filtered.length
  const start = (assetPagination.currentPage - 1) * assetPagination.pageSize
  assetList.value = filtered.slice(start, start + assetPagination.pageSize)
}

const applyEquipmentFiltersAndPaginate = () => {
  let filtered = allEquipmentData.value.slice()
  if (equipmentQuery.name) filtered = filtered.filter(item => item.name.includes(equipmentQuery.name))
  if (equipmentQuery.type) filtered = filtered.filter(item => item.type === equipmentQuery.type)
  if (equipmentQuery.status) filtered = filtered.filter(item => item.status === equipmentQuery.status)
  equipmentPagination.total = filtered.length
  const start = (equipmentPagination.currentPage - 1) * equipmentPagination.pageSize
  equipmentList.value = filtered.slice(start, start + equipmentPagination.pageSize)
}

const applyLandFiltersAndPaginate = () => {
  let filtered = allLandData.value.slice()
  if (landQuery.name) filtered = filtered.filter(item => item.name.includes(landQuery.name))
  if (landQuery.nature) filtered = filtered.filter(item => item.nature === landQuery.nature)
  if (landQuery.status) filtered = filtered.filter(item => item.status === landQuery.status)
  landPagination.total = filtered.length
  const start = (landPagination.currentPage - 1) * landPagination.pageSize
  landList.value = filtered.slice(start, start + landPagination.pageSize)
}

const applyIntangibleFiltersAndPaginate = () => {
  let filtered = allIntangibleData.value.slice()
  if (intangibleQuery.name) filtered = filtered.filter(item => item.name.includes(intangibleQuery.name))
  if (intangibleQuery.type) filtered = filtered.filter(item => item.type === intangibleQuery.type)
  intangiblePagination.total = filtered.length
  const start = (intangiblePagination.currentPage - 1) * intangiblePagination.pageSize
  intangibleList.value = filtered.slice(start, start + intangiblePagination.pageSize)
}

// 工具方法
const formatAmount = (amount: number | null | undefined): string => {
  if (amount === null || amount === undefined) return '0.00'
  return amount.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
}

// 固定资产相关方法
const getAssetTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    building: '建筑物',
    structure: '构筑物',
    machinery: '机械设备',
    transport: '运输设备',
    other: '其他'
  }
  return typeMap[type] || type
}

const getAssetTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    building: 'primary',
    structure: 'success',
    machinery: 'warning',
    transport: 'info',
    other: 'info'
  }
  return tagMap[type] || 'info'
}

const getAssetStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    normal: '正常使用',
    maintenance: '维修中',
    idle: '闲置',
    scrapped: '报废'
  }
  return statusMap[status] || status
}

const getAssetStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    normal: 'success',
    maintenance: 'warning',
    idle: 'info',
    scrapped: 'danger'
  }
  return tagMap[status] || 'info'
}

// 设备管理相关方法
const getEquipmentTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    monitoring: '监测设备',
    control: '控制设备',
    communication: '通信设备',
    safety: '安全设备',
    other: '其他设备'
  }
  return typeMap[type] || type
}

const getEquipmentTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    monitoring: 'primary',
    control: 'success',
    communication: 'warning',
    safety: 'danger',
    other: 'info'
  }
  return tagMap[type] || 'info'
}

const getEquipmentStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    running: '正常运行',
    fault: '故障',
    maintenance: '维护中',
    stopped: '停用'
  }
  return statusMap[status] || status
}

const getEquipmentStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    running: 'success',
    fault: 'danger',
    maintenance: 'warning',
    stopped: 'info'
  }
  return tagMap[status] || 'info'
}

// 土地管理相关方法
const getLandNatureText = (nature: string) => {
  const natureMap: Record<string, string> = {
    state_owned: '国有土地',
    collective: '集体土地',
    leased: '租赁土地'
  }
  return natureMap[nature] || nature
}

const getLandNatureTag = (nature: string) => {
  const tagMap: Record<string, string> = {
    state_owned: 'primary',
    collective: 'success',
    leased: 'warning'
  }
  return tagMap[nature] || 'info'
}

const getLandStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    normal: '正常使用',
    idle: '闲置',
    disputed: '争议中'
  }
  return statusMap[status] || status
}

const getLandStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    normal: 'success',
    idle: 'warning',
    disputed: 'danger'
  }
  return tagMap[status] || 'info'
}

// 无形资产相关方法
const getIntangibleTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    software: '软件著作权',
    patent: '专利权',
    trademark: '商标权',
    land_use: '土地使用权',
    other: '其他'
  }
  return typeMap[type] || type
}

const getIntangibleTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    software: 'primary',
    patent: 'success',
    trademark: 'warning',
    land_use: 'info',
    other: 'info'
  }
  return tagMap[type] || 'info'
}

const getIntangibleStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    valid: '有效',
    expired: '已过期',
    pending: '待审批'
  }
  return statusMap[status] || status
}

const getIntangibleStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    valid: 'success',
    expired: 'danger',
    pending: 'warning'
  }
  return tagMap[status] || 'info'
}

// 页面操作方法
const handleTabChange = (tabName: string) => {
  activeTab.value = tabName
  loadTabData(tabName)
}

const loadTabData = (tabName: string) => {
  switch (tabName) {
    case 'assets':
      loadAssetData()
      break
    case 'equipment':
      loadEquipmentData()
      break
    case 'land':
      loadLandData()
      break
    case 'intangible':
      loadIntangibleData()
      break
  }
}

// 固定资产操作方法
const loadAssetData = () => {
  // 初始化一次数据源并按查询与分页切片
  if (!allAssetData.value.length) {
    const types = ['building', 'structure', 'machinery', 'transport', 'other']
    const statuses = ['normal', 'maintenance', 'idle', 'scrapped']
    const locations = ['水库管理区', '大坝监测点', '库区西侧', '机房A']
    const count = 156
    const list: any[] = []
    for (let i = 0; i < count; i++) {
      const type = types[randInt(0, types.length - 1)]
      const status = statuses[randInt(0, statuses.length - 1)]
      const year = 2015 + randInt(0, 9)
      list.push({
        id: i + 1,
        code: `FA${String(i + 1).padStart(3, '0')}`,
        name: `资产-${i + 1}`,
        type,
        specification: ['钢筋混凝土结构','WL-2000型','HD-500型','通用规格'][randInt(0,3)],
        purchaseDate: `${year}-${String(randInt(1,12)).padStart(2,'0')}-${String(randInt(1,28)).padStart(2,'0')}`,
        originalValue: +(Math.random() * 200 + 10).toFixed(2),
        currentValue: +(Math.random() * 200 + 5).toFixed(2),
        status,
        location: locations[randInt(0, locations.length - 1)]
      })
    }
    allAssetData.value = list
  }
  applyAssetFiltersAndPaginate()
}

const searchAssets = () => {
  assetPagination.currentPage = 1
  applyAssetFiltersAndPaginate()
}

const resetAssetQuery = () => {
  Object.assign(assetQuery, { name: '', type: '', status: '' })
  assetPagination.currentPage = 1
  applyAssetFiltersAndPaginate()
}

const handleAddAsset = () => {
  ElMessage.info('新增固定资产功能开发中...')
}

const editAsset = (row: any) => {
  ElMessage.info(`编辑资产：${row.name}`)
}

const viewAssetDetail = (row: any) => {
  ElMessage.info(`查看资产详情：${row.name}`)
}

const viewAssetHistory = (row: any) => {
  ElMessage.info(`查看资产履历：${row.name}`)
}

const deleteAsset = async (row: any) => {
  try {
    await ElMessageBox.confirm(`确定要删除资产"${row.name}"吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('删除成功')
    loadAssetData()
  } catch {
    // 用户取消删除
  }
}

const exportAssets = () => {
  ElMessage.success('导出固定资产数据成功')
}

const handleAssetPagination = (pagination: any) => {
  assetPagination.currentPage = pagination.page
  assetPagination.pageSize = pagination.limit
  applyAssetFiltersAndPaginate()
}

// 设备管理操作方法
const loadEquipmentData = async () => {
  equipmentLoading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 300))
    const randInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
    const types = ['monitoring', 'control', 'communication', 'safety', 'other']
    const statuses = ['running', 'fault', 'maintenance', 'stopped']
    const locations = ['机房A', '泵站B', '闸室C', '库区D']
    const manufacturers = ['华为', '中兴', '海康', '大华', '西门子']
    const models = ['A100', 'B200', 'C300', 'D400', 'E500']
    const count = randInt(10, 50)
    const list: any[] = []
    for (let i = 0; i < count; i++) {
      const year = 2015 + randInt(0, 9)
      list.push({
        id: i + 1,
        code: `EQ-${year}-${String(i + 1).padStart(3, '0')}`,
        name: `设备-${i + 1}`,
        type: types[randInt(0, types.length - 1)],
        model: models[randInt(0, models.length - 1)],
        manufacturer: manufacturers[randInt(0, manufacturers.length - 1)],
        installDate: `${year}-${String(randInt(1, 12)).padStart(2, '0')}-${String(randInt(1, 28)).padStart(2, '0')}`,
        warrantyPeriod: randInt(12, 36),
        status: statuses[randInt(0, statuses.length - 1)],
        location: locations[randInt(0, locations.length - 1)]
      })
    }
    allEquipmentData.value = list
    applyEquipmentFiltersAndPaginate()
  } finally {
    equipmentLoading.value = false
  }
}

const searchEquipment = () => {
  equipmentPagination.currentPage = 1
  applyEquipmentFiltersAndPaginate()
}

const resetEquipmentQuery = () => {
  Object.assign(equipmentQuery, { name: '', type: '', status: '' })
  equipmentPagination.currentPage = 1
  applyEquipmentFiltersAndPaginate()
}

const handleAddEquipment = () => {
  ElMessage.info('新增设备功能开发中...')
}

const editEquipment = (row: any) => {
  ElMessage.info(`编辑设备：${row.name}`)
}

const viewEquipmentDetail = (row: any) => {
  ElMessage.info(`查看设备详情：${row.name}`)
}

const viewMaintenanceRecord = (row: any) => {
  ElMessage.info(`查看维护记录：${row.name}`)
}

const deleteEquipment = async (row: any) => {
  try {
    await ElMessageBox.confirm(`确定要删除设备"${row.name}"吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('删除成功')
    loadEquipmentData()
  } catch {
    // 用户取消删除
  }
}

const exportEquipment = () => {
  ElMessage.success('导出设备数据成功')
}

const handleEquipmentPagination = (pagination: any) => {
  equipmentPagination.currentPage = pagination.page
  equipmentPagination.pageSize = pagination.limit
  applyEquipmentFiltersAndPaginate()
}

// 土地管理操作方法
const loadLandData = async () => {
  landLoading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 300))
    if (!allLandData.value.length) {
      const natures = ['state_owned', 'collective', 'leased']
      const statuses = ['normal', 'idle', 'disputed']
      const useTypes = ['水利设施', '绿化用地', '仓储用地', '办公区', '其他']
      const locations = ['库区东侧', '库区西侧', '管理中心', '闸门旁', '泵站附近']
      const count = randInt(10, 50)
      const list: any[] = []
      for (let i = 0; i < count; i++) {
        const year = 2010 + randInt(0, 14)
        list.push({
          id: i + 1,
          code: `LD-${String(i + 1).padStart(3, '0')}`,
          name: `地块-${i + 1}`,
          nature: natures[randInt(0, natures.length - 1)],
          area: randInt(10, 300),
          location: locations[randInt(0, locations.length - 1)],
          useType: useTypes[randInt(0, useTypes.length - 1)],
          certificateNumber: `CERT-${String(i + 1).padStart(4, '0')}`,
          acquisitionDate: `${year}-${String(randInt(1, 12)).padStart(2, '0')}-${String(randInt(1, 28)).padStart(2, '0')}`,
          status: statuses[randInt(0, statuses.length - 1)]
        })
      }
      allLandData.value = list
    }
    applyLandFiltersAndPaginate()
  } finally {
    landLoading.value = false
  }
}

const searchLand = () => {
  landPagination.currentPage = 1
  applyLandFiltersAndPaginate()
}

const resetLandQuery = () => {
  Object.assign(landQuery, { name: '', nature: '', status: '' })
  landPagination.currentPage = 1
  applyLandFiltersAndPaginate()
}

const handleAddLand = () => {
  ElMessage.info('新增土地功能开发中...')
}

const editLand = (row: any) => {
  ElMessage.info(`编辑土地：${row.name}`)
}

const viewLandDetail = (row: any) => {
  ElMessage.info(`查看土地详情：${row.name}`)
}

const viewLandCertificate = (row: any) => {
  ElMessage.info(`查看土地证书：${row.name}`)
}

const deleteLand = async (row: any) => {
  try {
    await ElMessageBox.confirm(`确定要删除土地"${row.name}"吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('删除成功')
    loadLandData()
  } catch {
    // 用户取消删除
  }
}

const exportLand = () => {
  ElMessage.success('导出土地数据成功')
}

const handleLandPagination = (pagination: any) => {
  landPagination.currentPage = pagination.page
  landPagination.pageSize = pagination.limit
  applyLandFiltersAndPaginate()
}

// 无形资产操作方法
const loadIntangibleData = async () => {
  intangibleLoading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 300))
    if (!allIntangibleData.value.length) {
      const types = ['software', 'patent', 'trademark', 'land_use', 'other']
      const statuses = ['valid', 'expired', 'pending']
      const count = randInt(10, 50)
      const list: any[] = []
      for (let i = 0; i < count; i++) {
        const year = 2014 + randInt(0, 10)
        list.push({
          id: i + 1,
          code: `IA-${String(i + 1).padStart(4, '0')}`,
          name: `无形资产-${i + 1}`,
          type: types[randInt(0, types.length - 1)],
          acquisitionDate: `${year}-${String(randInt(1, 12)).padStart(2, '0')}-${String(randInt(1, 28)).padStart(2, '0')}`,
          originalValue: randInt(50, 500),
          amortizationPeriod: randInt(5, 20),
          remainingValue: randInt(10, 300),
          expiryDate: `${year + randInt(1, 10)}-${String(randInt(1, 12)).padStart(2, '0')}-${String(randInt(1, 28)).padStart(2, '0')}`,
          status: statuses[randInt(0, statuses.length - 1)]
        })
      }
      allIntangibleData.value = list
    }
    applyIntangibleFiltersAndPaginate()
  } finally {
    intangibleLoading.value = false
  }
}

const searchIntangible = () => {
  intangiblePagination.currentPage = 1
  applyIntangibleFiltersAndPaginate()
}

const resetIntangibleQuery = () => {
  Object.assign(intangibleQuery, { name: '', type: '' })
  intangiblePagination.currentPage = 1
  applyIntangibleFiltersAndPaginate()
}

const handleAddIntangible = () => {
  ElMessage.info('新增无形资产功能开发中...')
}

const editIntangible = (row: any) => {
  ElMessage.info(`编辑无形资产：${row.name}`)
}

const viewIntangibleDetail = (row: any) => {
  ElMessage.info(`查看无形资产详情：${row.name}`)
}

const viewIntangibleCertificate = (row: any) => {
  ElMessage.info(`查看无形资产证书：${row.name}`)
}

const deleteIntangible = async (row: any) => {
  try {
    await ElMessageBox.confirm(`确定要删除无形资产"${row.name}"吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('删除成功')
    loadIntangibleData()
  } catch {
    // 用户取消删除
  }
}

const exportIntangible = () => {
  ElMessage.success('导出无形资产数据成功')
}

const handleIntangiblePagination = (pagination: any) => {
  intangiblePagination.currentPage = pagination.page
  intangiblePagination.pageSize = pagination.limit
  applyIntangibleFiltersAndPaginate()
}

// 页面初始化
onMounted(() => {
  loadTabData(activeTab.value)
})
</script>

<style scoped>
.property-registration {
  padding: 20px;
}

.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 8px 0;
}

.page-description {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.stats-section {
  margin-bottom: 24px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  background: linear-gradient(135deg, #409eff, #66b1ff);
  color: white;
  font-size: 24px;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  color: #909399;
  font-size: 14px;
}

.main-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.tab-content {
  padding: 20px;
}

.query-section {
  padding: 16px;
  background-color: #f5f7fa;
  border-radius: 4px;
  margin-bottom: 16px;
}

.action-section {
  margin-bottom: 16px;
}

.pagination-section {
  margin-top: 16px;
  text-align: right;
}

.amount {
  font-weight: 600;
  color: #e6a23c;
}

:deep(.el-tabs__header) {
  margin: 0;
  border-bottom: 1px solid #e4e7ed;
}

:deep(.el-tabs__nav-wrap) {
  padding: 0 20px;
}

:deep(.el-tabs__item) {
  height: 50px;
  line-height: 50px;
  font-weight: 500;
}

:deep(.el-table) {
  border-radius: 4px;
}

:deep(.el-table th) {
  background-color: #fafafa;
  font-weight: 600;
}

:deep(.el-pagination) {
  justify-content: flex-end;
}
</style>